<template>
  <div class="order-item">
    <div class="head">
      <span>下单时间：{{ order.createTime }}</span>
      <span>订单编号：{{ order.id }}</span>
      <!-- 订单状态为未支付才会显示付款截止时间且没超出 -->
      <span class="down-time" v-if="order.orderState === 1 && order.countdown !== -1">
        <i class="iconfont icon-down-time"></i>
        <b>付款截止：{{ timeText }}</b>
      </span>
      <!-- 订单状态为已完成(5)或者是取消(6)的订单时，显示删除 -->
      <!-- 删除数据直接去父组件删除，通过$emit触发父组件的数据 -->
      <a v-if="[5, 6].includes(order.orderState) || (order.orderState === 1 && order.countdown === -1)"
        href="javascript:;" class="del" @click="delOrder(order.id)">删除</a>
    </div>
    <div class="body">
      <div class="column goods">
        <ul>
          <li v-for="goods in order.skus" :key="goods.id">
            <a class="image" href="javascript:;">
              <img :src="goods.image" alt="" />
            </a>
            <div class="info">
              <p class="name ellipsis-2">{{ goods.name }}</p>
              <p class="attr ellipsis">
                {{ goods.attrsText }}
              </p>
            </div>
            <div class="price">¥{{ goods.realPay.toFixed(2) }}</div>
            <div class="count">x{{ goods.quantity }}</div>
          </li>
        </ul>
      </div>
      <div class="column state">
        <!-- 订单状态 -->
        <!-- 超过30分钟未付款的订单 -->
        <p v-if="order.orderState === 1 && order.countdown === -1">订单已超时</p>
        <!-- 正常的订单 -->
        <p v-else>{{ orderStatusName[order.orderState] }}</p>
        <!-- 待收货：查看物流 -->
        <!-- 待评价：评价商品 -->
        <!-- 已完成：查看评价 -->
        <p v-if="order.orderState === 3"><a @click="openOrderExpress(order.id)" href="javascript:;" class="green">查看物流</a>
        </p>
        <p v-if="order.orderState === 4"><a href="javascript:;" class="green">评价商品</a></p>
        <p v-if="order.orderState === 5"><a href="javascript:;" class="green">查看评价</a></p>
      </div>
      <div class="column amount">
        <p class="red">¥{{ order.payMoney.toFixed(2) }}</p>
        <p>（含运费：¥{{ order.postFee.toFixed(2) }}）</p>
        <p>{{ order.payType === 1 ? `在线支付(${order.payChannel === 1 ? '支付宝' : '微信'})` : '货到付款' }}</p>
      </div>
      <div class="column action">
        <!-- 待支付：立即付款，查看详情，取消订单 -->
        <!-- 待发货：查看详情，再次购买 -->
        <!-- 待收货：确认收货，查看详情，再次购买 -->
        <!-- 待评价：查看详情，再次购买，申请售后 -->
        <!-- 已完成：查看详情，再次购买，申请售后 -->
        <!-- 已取消：查看详情 -->
        <xtx-button v-if="order.orderState === 1 && order.countdown !== -1" type="primary" size="small" @click="$router.push('/member/pay?id='+order.id)">立即付款</xtx-button>
        <xtx-button @click="confirmReceipt(order.id)" v-if="order.orderState === 3" type="primary"
          size="small">确认收货</xtx-button>
        <p>
          <RouterLink :to="`/member/order/${order.id}`">查看详情</RouterLink>
        </p>
        <p v-if="order.orderState === 1 && order.countdown !== -1"><a href="javascript:;"
            @click="openOrderCancel">取消订单</a></p>
        <p v-if="[2, 3, 4, 5].includes(order.orderState)">
          <RouterLink :to="`/member/checkout?id=${order.id}`">再次购买</RouterLink>
        </p>
        <p v-if="[4, 5].includes(order.orderState)"><a href="javascript:;">申请售后</a></p>
      </div>
    </div>
  </div>
  <!-- 取消订单的组件 -->
  <OrderCancel :orderId="order.id" ref="orderCancelCom"></OrderCancel>
  <!-- 查看物流 -->
  <OrderExpress ref="orderExpressCom"></OrderExpress>
</template>
<script setup>
import { defineProps, provide, ref, watch, defineEmits } from 'vue'
import { orderStatusName } from '@/config'
// 导入订单倒计时的函数
import { usePayTime } from '@/hook'
import { receiptOrderApi } from '@/api/order'
import Confirm from '@/components/library/confirm'
import Message from '@/components/library/message'
import OrderCancel from './order-cancel.vue'
import OrderExpress from './order-express.vue'
// 父组件要触发的自定义事件
const emit = defineEmits(['order-del'])
const { start, timeText } = usePayTime()
const props = defineProps({
  order: {
    type: Object,
    default: () => ({})
  }
})
// 单条订单数据
const order = ref(null)
watch(() => props.order, newOrder => {
  order.value = newOrder
  // 提供order数据给其他的子组件使用
  provide('order', newOrder)
  if (newOrder.countdown !== -1) {
    // 开启倒计时
    start(newOrder.countdown)
  }
}, { immediate: true })

// 取消订单
const orderCancelCom = ref(null)
function openOrderCancel() {
  // 调用组件的方法,打开对话框
  orderCancelCom.value.open()
}
// 确认收货
function confirmReceipt(orderId) {
  Confirm({ title: '确认收货提示', text: '您确认收到货吗？确认后货款将会打给卖家' }).then(() => {
    receiptOrderApi(orderId).then(() => {
      Message({ type: 'success', text: '操作成功' })
      // 更改订单状态
      order.value.orderState = 4
    }).catch(err => {
      Message({ type: 'error', text: err.response.data.message })
    })
  })
}
// 查看物流
const orderExpressCom = ref(null)
function openOrderExpress(orderId) {
  orderExpressCom.value.open(orderId)
}
// 删除订单
function delOrder(orderId) {
  emit('order-del', orderId)
}

</script>
<style lang="less" scoped>
.order-item {
  margin-bottom: 20px;
  border: 1px solid #f5f5f5;

  .head {
    height: 50px;
    line-height: 50px;
    background: #f5f5f5;
    padding: 0 20px;
    overflow: hidden;

    span {
      margin-right: 20px;

      &.down-time {
        margin-right: 0;
        float: right;

        i {
          vertical-align: middle;
          margin-right: 3px;
        }

        b {
          vertical-align: middle;
          font-weight: normal;
        }
      }
    }

    .del {
      margin-right: 0;
      float: right;
      color: #999;
    }
  }

  .body {
    display: flex;
    align-items: stretch;

    .column {
      border-left: 1px solid #f5f5f5;
      text-align: center;
      padding: 20px;

      >p {
        padding-top: 10px;
      }

      &:first-child {
        border-left: none;
      }

      &.goods {
        flex: 1;
        padding: 0;
        align-self: center;

        ul {
          li {
            border-bottom: 1px solid #f5f5f5;
            padding: 10px;
            display: flex;

            &:last-child {
              border-bottom: none;
            }

            .image {
              width: 70px;
              height: 70px;
              border: 1px solid #f5f5f5;
            }

            .info {
              width: 220px;
              text-align: left;
              padding: 0 10px;

              p {
                margin-bottom: 5px;

                &.name {
                  height: 38px;
                }

                &.attr {
                  color: #999;
                  font-size: 12px;

                  span {
                    margin-right: 5px;
                  }
                }
              }
            }

            .price {
              width: 100px;
            }

            .count {
              width: 80px;
            }
          }
        }
      }

      &.state {
        width: 120px;

        .green {
          color: @xtxColor;
        }
      }

      &.amount {
        width: 200px;

        .red {
          color: @priceColor;
        }
      }

      &.action {
        width: 140px;

        a {
          display: block;

          &:hover {
            color: @xtxColor;
          }
        }
      }
    }
  }
}
</style>